// pages/TvDetails/TvDetails.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
        TvOff: true, //储存的是点赞的开关
        TvUserId: '', //储存的是指定视频的ID
        tokens: '', //储存的获取到的token
        TvList: {}, //储存的是获取到的视频的详情
        commentsList: [], //储存的是获取到的评论
        detailText: '', //储存的是热评的值
    },
    // 发布评论
    release() {
        var that =this
        // console.log(this.data.detailText);
        wx.request({
            data: {
                content: this.data.detailText
            },
            header: {
                'Authorization': this.data.tokens,
                "Content-Type": "application/x-www-form-urlencoded",
            },
            method: 'POST',
            url: `http://127.0.0.1:7001/api/v1/videos/${this.data.TvUserId}/comments`,
            success(res) {
                console.log(res);
                if (res.statusCode == 200) {
                    wx.showToast({
                        title: '评论成功',
                        icon: 'success',
                        duration: 2000
                    })
                    that.postUserComments()
                    that.setData({
                        detailText:''
                    })
                } else {
                    wx.showToast({
                        title: res.data.error,
                        icon: 'success',
                        duration: 2000
                    })
                }
            }
        })
    },
    // 获取评论的文字
    releaseText(e) {
        this.setData({
            detailText: e.detail.value
        })
    },
    // 删除评论
    delete(e) {
        // console.log(this.data.tokens);
        var that = this
        wx.request({
            data: {
                videoId: that.data.TvUserId,
            },
            method: 'DELETE',
            header: {
                'Authorization': that.data.tokens,
                'content-type': 'application/x-www-form-urlencoded'
            },
            url: `http://127.0.0.1:7001/api/v1/videos/${that.data.TvUserId}/comments/${e.currentTarget.dataset.id}`,
            success(res) {
                console.log(res);
                if (res.statusCode == 204) {
                    wx.showToast({
                        title: '删除评论成功',
                        icon: 'success',
                        duration: 2000
                    })
                    that.postUserComments()
                } else {
                    wx.showToast({
                        title: res.data.error,
                        icon: 'error',
                        duration: 2000
                    })
                }
            }
        })
    },
    // 点赞视频
    goodTv() {
        var that = this
        wx.request({
            data: {
                videoId: this.data.TvUserId
            },
            method: 'POST',
            header: {
                'Authorization': this.data.tokens,
                'content-type': 'application/x-www-form-urlencoded'
            },
            url: `http://127.0.0.1:7001/api/v1/videos/${this.data.TvUserId}/like`,
            success(res) {
                console.log(res);
                if (res.statusCode == 200) {
                    wx.showToast({
                        title: '收藏视频成功',
                        icon: 'success',
                        duration: 2000
                    })
                    that.setData({
                        TvOff: false
                    })
                }
            }
        })
    },
    // 取消视频点赞
    NogoogTv() {
        var that = this
        wx.request({
            data: {
                videoId: this.data.TvUserId
            },
            method: 'POST',
            header: {
                'Authorization': this.data.tokens,
                'content-type': 'application/x-www-form-urlencoded'
            },
            url: `http://127.0.0.1:7001/api/v1/videos/${this.data.TvUserId}/like`,
            success(res) {
                // console.log(res);
                if (res.statusCode == 200) {
                    wx.showToast({
                        title: '取消收藏成功',
                        icon: 'success',
                        duration: 2000
                    })
                    that.setData({
                        TvOff: true
                    })
                }
            }
        })
    },
    // 跳转到视频UP主的详情页面
    details(id) {
        console.log(id.currentTarget.dataset.id);
        wx.navigateTo({
            url: '../details/details?id=' + id.currentTarget.dataset.id,
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
        var that = this
        // console.log(options);
        // 设置id值
        this.setData({
            TvUserId: options.id
        })
        // 获取token值
        var key = wx.getStorageSync('token')
        // 设置token值
        this.setData({
            tokens: key.data.token
        })
        // 视频详情
        wx.request({
            data: {
                videoId: options.id
            },
            method: 'GET',
            header: {
                'Authorization': key.data.token,
                'content-type': 'application/x-www-form-urlencoded'
            },
            url: `http://127.0.0.1:7001/api/v1/video/` + options.id,
            success(res) {
                // console.log(res);
                that.setData({
                    TvList: res.data.videoInfo
                })
            }
        })
        // 调用评论的函数
        that.postUserComments()

    },
    postUserComments() {
        var that= this
        // 评论列表
        wx.request({
            data: {
                pageNum:1,
                pageSize:999999999,
            },
            header: {},
            method: 'GET',
            url: `http://127.0.0.1:7001/api/v1/videos/${that.data.TvUserId}/comments`,
            success(res) {
                console.log(res);
                that.setData({
                    commentsList: res.data.comments
                })
            }
        })
    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})